<template>
  <a-input type="file" accept="" @change="submitFile" />
</template>

<script lang="ts" setup>
  import { message } from 'ant-design-vue';
  import { useData } from '@/hooks/useData';

  const emit = defineEmits(['finish']);

  const { saveData } = useData();

  function submitFile(e: any) {
    var reader = new FileReader(); //新建一个FileReader
    reader.readAsText(e.target.files[0], 'UTF-8'); //读取文件

    reader.onload = async function (evt) {
      if (evt.target?.result) {
        try {
          await saveData(evt.target.result);
          emit('finish');
        } catch (e) {
          message.error(e as string);
        }
      } else {
        message.error('无法读取文件, 请检查格式');
      }
    };
  }
</script>

<style lang="less" scoped></style>
